<script>
  var mergeTwoLists = function(list1, list2) {
    let x = list1;
    let y = list2;

    let dummy = new ListNode();
    let head = dummy;

    while (x !== null && y !== null) {
      if (x.val < y.val) {
        dummy.next = x;
        x = x.next;
        dummy = dummy.next;
      } else {
        dummy.next = y;
        y = y.next;
        dummy = dummy.next;
      }
    }

    if (x !== null) {
      dummy.next = x;
    }
    if (y !== null) {
      dummy.next = y;
    }

    return head.next;
  };
</script>